<?php

/**
 * Expose user as a context condition.
 */
class context_extend_condition_user extends context_condition {
  function condition_values() {
    $values = array();
    $values['own_user'] = t('Own user');
    return $values;
  }

  function options_form($context) {
    $defaults = $this->fetch_from_context($context, 'options');
    return array(
      'uid' => array(
        '#title' => t('Active for UID'),
        '#type' => 'textfield',
        '#size' => 20,
        '#default_value' => isset($defaults['uid']) ? $defaults['uid'] : 0,
        '#attributes' => array('readonly' => 'readonly')
      ),
    );
  }

  function execute($account, $op) {
    foreach ($this->get_contexts($op) as $context) {
      $options = $this->fetch_from_context($context, 'options');
      $uid = !empty($options['uid']) ? $options['uid'] : '';
      $user = user_load($uid);
      if ($user) {
        if ($account->uid == $user->uid) {
          $this->condition_met($context);
        }
      }
    }
  }
}
